package com.imay.live.fragment.load;

import android.graphics.Color;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.AnimationDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.imay.cocos.IMayCocosNetManager;
import com.imay.live.R;
import com.imay.live.activity.BaseUpdateActivtiy;
import com.imay.live.cache.AdsCache;
import com.imay.live.cache.BannerCache;
import com.imay.live.cache.LabelCache;
import com.imay.live.cache.PastePhotoCache;
import com.imay.live.fragment.BaseRequestFragment;
import com.imay.live.fragment.user.MainUserListener;
import com.imay.live.fragment.user.UserFragment;
import com.imay.live.manager.AccountManager;
import com.imay.live.view.WebpView;

/**
 * Created by Administrator on 2017/5/21 0021.
 */

public class LoadMokeyFragment extends BaseRequestFragment {
    public static final int IN_RUNNING = 1001;
    public static final int IN_ENDING = 1002;
    private long timeCover = 4000; //总时长
    private int pertime = 25; // 每次滑动时长

    public static LoadMokeyFragment getInstance() {
        return new LoadMokeyFragment();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_load;
    }

    WebpView imageView;
/*
    boolean canstart = false;//这是广告倒计时是否到 别改动 影响升级弹窗
    boolean cosstart = false;//这是cocos 升级 别改动 影响升级弹窗
*/


    @Override
    protected void initView(Bundle savedInstanceState) {
        imageView = $(R.id.animation_iv);
        /*DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(Uri.parse("asset:///mokey_shake.webp"))
                .setAutoPlayAnimations(true)
                .build();
        imageView.setController(controller);*/
        //imageView.setUri(Uri.parse("asset:///mokey_shake.webp"), true, 116, 106);


        ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
            @Override
            public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) {
                if (anim != null) {
                    $(R.id.load_image).setVisibility(View.GONE);
                    anim.start();
                    startActivity();
                }
            }

            @Override
            public void onFailure(String id, Throwable throwable) {
                super.onFailure(id, throwable);
                startActivity();
            }
        };

        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse("asset:///mokey_shake.webp"))
                .setResizeOptions(new ResizeOptions(50, 50)).build();

        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(Uri.parse("asset:///mokey_shake.webp"))
                .setControllerListener(controllerListener)
                .setImageRequest(request)
                .build();
        imageView.setController(controller);
    }

    private void startActivity() {
        new BannerCache(null).updateCache();
        //new PastePhotoCache(null).updateCache();
//        new AdsCache(null).updateCache();
        new LabelCache(null).updateCache();

        mCodeHandler.sendEmptyMessageDelayed(IN_ENDING, timeCover);
    }

    /**
     * 倒计时Handler
     */
    Handler mCodeHandler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == IN_RUNNING) {// 正在倒计时
                mCodeHandler.sendEmptyMessageDelayed(IN_RUNNING, pertime);
            } else if (msg.what == IN_ENDING) {
//                canstart = true;
                startHome();
            }
        }
    };


    private synchronized void startHome() {
        if (getActivity() instanceof MainUserListener) {
            ((MainUserListener) getActivity()).start();
        }
    }


    @Override
    protected boolean useDynamicTitleBar() {
        return false;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (imageView != null) {
            imageView.destroyDrawingCache();
            imageView = null;
        }
        if (mCodeHandler != null) {
            mCodeHandler.removeCallbacksAndMessages(null);
        }
    }
}